package com.zy.yun.provider.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.zy.yun.base.entity.User;
import com.zy.yun.provider.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: 王彦委
 * @Description: 校验用户名 和 密码
 * @Date: 12:28 2019/10/27
 */
@Service("userDetailsService")
public class UserDetailsServiceImpl implements UserDetailsService {

    @Autowired
    private IUserService userService;

    @Override
    public UserDetails loadUserByUsername(String name) throws UsernameNotFoundException {

        //去数据库，查询用户信息
        EntityWrapper<User> userQueryWrapper = new EntityWrapper<>();
        userQueryWrapper.eq("name", name);

        User user = userService.selectOne(userQueryWrapper);



        List<GrantedAuthority> authorities = new ArrayList();

        GrantedAuthority simpleGrantedAuthority = new SimpleGrantedAuthority("/res/getMenu");
        authorities.add(simpleGrantedAuthority);


        return new org.springframework.security.core.userdetails.User(user.getName(),user.getPassword(), authorities);
    }
}
